package cn.ultrarisk.scadmin.manager.web.controller.system;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrCollectionDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrCollection;
import cn.ultrarisk.scadmin.manager.biz.user.dao.AdminDAO;
import cn.ultrarisk.scadmin.manager.biz.user.dao.RoleDAO;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Admin;
import cn.ultrarisk.scadmin.manager.biz.user.service.AdminService;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.system.AdminDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * AdminController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/system/admin", produces = "application/json")
public class AdminController extends BaseController {
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private SolrCollectionDAO solrCollectionDAO;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<Admin>> list(String account, String name, int pageNo, int pageSize) {
        Query query = Query.likeFullQuery(Admin.ACCOUNT, account).likeFull(Admin.NAME, name);
        query.addSort(Admin.CREATE_TIME, Query.Direction.DESC);
        Page<Admin> admins = adminDAO.findPage(query, pageNo, pageSize);
        return getOkResponseEntity(admins);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(@RequestBody Admin admin) {
        adminService.create(admin.getAccount(), admin.getPassword(), admin.getName(), admin.getEmail());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) {
        adminService.delete(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/disable", method = RequestMethod.POST)
    public ResponseEntity<String> disable(@PathVariable("id") Integer id) {
        adminService.disable(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/enable", method = RequestMethod.POST)
    public ResponseEntity<String> enable(@PathVariable("id") Integer id) {
        adminService.enable(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/disableAll", method = RequestMethod.POST)
    public ResponseEntity<String> disableAll(@RequestBody AdminDTO dto) {
        adminService.disable(dto.getIds());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/enableAll", method = RequestMethod.POST)
    public ResponseEntity<String> enableAll(@RequestBody AdminDTO dto) {
        adminService.enable(dto.getIds());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
    public ResponseEntity<String> deleteAll(@RequestBody AdminDTO dto) {
        adminService.delete(dto.getIds());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/getByAccount", method = RequestMethod.GET)
    public ResponseEntity<Admin> getByAccount(String account) {
        return getOkResponseEntity(adminDAO.getByAccount(account));
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/privilege", method = RequestMethod.GET)
    public ResponseEntity<Map<String, Object>> getPrivilege(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("adminRoles", roleDAO.findRoleByAdmin(id));
        map.put("adminCollections", roleDAO.findCollectionRoleByAdmin(id));
        map.put("roles", roleDAO.findList(new Query()));
        map.put("collections", solrCollectionDAO.findList(new Query(), SolrCollection.ID, SolrCollection.NAME));
        return getOkResponseEntity(map);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/privilege", method = RequestMethod.POST)
    public ResponseEntity<String> savePrivilege(@PathVariable("id") Integer id, @RequestBody AdminDTO dto) {
        adminService.saveRole(id, dto.getRoles(), dto.getCollectionRoles());
        return getOkResponseEntity();
    }
}
